package com.wsq.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.wsq.calculate.Calculate;
import com.wsq.calculate.R;

public class ColorCheckbox extends CheckBox implements OnCheckedChangeListener {

	private OnCheckedChangeListener myListener;

	private int mTextColor;
	private Paint mPaint;

	public ColorCheckbox(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray a = context
				.obtainStyledAttributes(attrs, R.styleable.MyText);
		mTextColor = a.getColor(0,
				context.getResources().getColor(R.color.btn_text_color1));
		a.recycle();
		mPaint = getPaint();
		mPaint.setColor(mTextColor);
		Calculate calculate = (Calculate) context;
		calculate.adjustFontSize(this);
		myListener = calculate.eventListener;
		setOnCheckedChangeListener(this);
	}

	public ColorCheckbox(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		myListener.onCheckedChanged(buttonView, isChecked);
	}

	@Override
	protected void onTextChanged(CharSequence text, int start,
			int lengthBefore, int lengthAfter) {
		// TODO Auto-generated method stub
		measureText();
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		// TODO Auto-generated method stub
		measureText();
	}

	private float mTextX;
	private float mTextY;

	private void measureText() {
		mTextX = (getWidth() - getPaint().measureText(getText().toString())) / 2;
		mTextY = (getHeight() - getPaint().ascent() - getPaint().descent()) / 2;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		CharSequence text = getText();
		canvas.drawText(text, 0, text.length(), mTextX, mTextY, mPaint);
	}

}
